package com.avo.ramcache.util;

import org.springframework.scheduling.support.CronExpression;
import org.springframework.scheduling.support.CronSequenceGenerator;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.TimeZone;

public class DateUtils {

    @Deprecated
    public static Date getNextTime(String cron, Date now) {
        CronSequenceGenerator gen = new CronSequenceGenerator(cron, TimeZone.getDefault());
        return gen.next(now);
    }

    public static long getNextTime(String cron) {
        CronExpression parse = CronExpression.parse(cron);
        LocalDateTime localDateTime = parse.next(LocalDateTime.now());
        assert localDateTime != null;
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

}
